package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.food.client.foodskuclient.FoodSkuQueryClient;
import com.woniuxy.food.client.foodspuclient.FoodSpuQueryClient;
import com.woniuxy.food.client.result.FoodSkuClientDto;
import com.woniuxy.food.client.result.FoodSpuClientDto;
import com.woniuxy.member.client.CommentManageClient;
import com.woniuxy.orderService.client.OrderManageClient;
import com.woniuxy.orderService.client.OrderQueryClient;
import com.woniuxy.orderService.client.param.RoleStoreQueryFoodReportParam;
import com.woniuxy.orderService.client.param.RoleStoreQuerySalesReportParam;
import com.woniuxy.orderService.client.param.RoleStoreQueryTableReportParam;
import com.woniuxy.orderService.client.result.*;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.OrderClient;
import com.woniuxy.store.client.param.orderParam.PayOffParams;
import com.woniuxy.store.client.param.orderParam.RoleStoreQueryFoodReportParams;
import com.woniuxy.store.client.param.orderParam.RoleStoreQuerySalesReportParams;
import com.woniuxy.store.client.param.orderParam.RoleStoreQueryTableReportParams;
import com.woniuxy.table.client.TableQueryClient;
import com.woniuxy.table.client.result.TableClientDto;
import common.WoniuPage;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.CommonResultCodeEnum;
import result.Result;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/store/order")
public class OrderClientImpl implements OrderClient {
    @Resource
    OrderManageClient orderManageClient;
    @Autowired
    OrderQueryClient orderQueryClient;
    @Resource
    FoodSkuQueryClient foodSkuQueryClient;
    @Resource
    FoodSpuQueryClient foodSpuQueryClient;
    @Autowired
    TableQueryClient tableQueryClient;


    @Override
    public Result reduceFood(Integer itemId, Integer skuCount) {
        Result<OrderItemDto> orderItemDtoResult = orderQueryClient.queryOrderItemByItemId(itemId);
        if (orderItemDtoResult.getData()==null){
            throw new MallException("订单详情不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        if (orderItemDtoResult.getData().getSkuCount()<skuCount) {
            throw new MallException("退菜失败", CommonResultCodeEnum.INVALID_PARAM);
        }
        Result<FoodSkuClientDto> foodSkuClientDtoResult = foodSkuQueryClient.searchFoodSkuById(orderItemDtoResult.getData().getSkuId());
        Result<FoodSpuClientDto> foodSpuClientDtoResult = foodSpuQueryClient.searchFoodSpuBySkuId(foodSkuClientDtoResult.getData().getSkuId());
        if (foodSpuClientDtoResult.getData().getStoreId()!= StoreIdUtil.getFromToken()) {
            throw new MallException("退菜失败", CommonResultCodeEnum.INVALID_PARAM);
        }
        orderManageClient.reduceFood(itemId,skuCount);
        return Result.success();
    }


    @Override
    public void pay(String payOrderNum) throws Exception {
        Result<PayOrderDto> payOrderDtoResult = orderQueryClient.queryPayOrderByPayOrderNum(payOrderNum);
        if (!payOrderDtoResult.getData().getPayState().equals("OFFLINE")) {
            throw new MallException("支付失败",CommonResultCodeEnum.INVALID_PARAM);
        }

        orderManageClient.pay(payOrderNum);
    }

    @Override
    public Result queryStoreOrder(Integer tableId) {
        Result<TableClientDto> storeTableByTable = tableQueryClient.getStoreTableByTable(tableId);
        if (storeTableByTable.getData().getStoreId()!= StoreIdUtil.getFromToken()) {
            throw new MallException("该桌子不存在",CommonResultCodeEnum.INVALID_PARAM);
        }
        Result<List<QueryStoreClientOrderDto>> listResult = orderQueryClient.queryStoreOrder(tableId);
        List<QueryStoreClientOrderDto> data = listResult.getData();
        return Result.success(data);
    }

    @Override
    public Result storeQueryStoreOrder(Integer pageNum, Integer pageSize) {
        Result<WoniuPage<QueryStoreClientOrderDto>> woniuPageResult = orderQueryClient.storeQueryStoreOrder(StoreIdUtil.getFromToken(), pageNum, pageSize);
        WoniuPage<QueryStoreClientOrderDto> data = woniuPageResult.getData();
        return Result.success(data);
    }

    @Override
    public Result roleStoreQuerySalesReport(@Valid RoleStoreQuerySalesReportParams roleStoreQuerySalesReportParams) {
        RoleStoreQuerySalesReportParam roleStoreQuerySalesReportParam = BeanUtil.toBean(roleStoreQuerySalesReportParams, RoleStoreQuerySalesReportParam.class);
        roleStoreQuerySalesReportParam.setStoreId(StoreIdUtil.getFromToken());
        Result<WoniuPage<StoreSalesReportDto>> woniuPageResult = orderQueryClient.roleStoreQuerySalesReport(roleStoreQuerySalesReportParam);
        WoniuPage<StoreSalesReportDto> data = woniuPageResult.getData();
        return Result.success(data);
    }

    @Override
    public Result roleStoreQueryTableReport(RoleStoreQueryTableReportParams roleStoreQueryTableReportParams) {
        RoleStoreQueryTableReportParam roleStoreQueryTableReportParam = BeanUtil.toBean(roleStoreQueryTableReportParams, RoleStoreQueryTableReportParam.class);
        roleStoreQueryTableReportParam.setStoreId(StoreIdUtil.getFromToken());
        Result<WoniuPage<TableReportDto>> woniuPageResult = orderQueryClient.roleStoreQueryTableReport(roleStoreQueryTableReportParam);
        WoniuPage<TableReportDto> data = woniuPageResult.getData();
        return Result.success(data);
    }

    @Override
    public Result roleStoreQueryFoodReport(@Valid RoleStoreQueryFoodReportParams roleStoreQueryFoodReportParams) {
        RoleStoreQueryFoodReportParam roleStoreQueryFoodReportParam = BeanUtil.toBean(roleStoreQueryFoodReportParams, RoleStoreQueryFoodReportParam.class);
        roleStoreQueryFoodReportParam.setStoreId(StoreIdUtil.getFromToken());
        Result<WoniuPage<FoodReportDto>> listResult = orderQueryClient.roleStoreQueryFoodReport(roleStoreQueryFoodReportParam);
        WoniuPage<FoodReportDto> data = listResult.getData();
        return Result.success(data);
    }


    @Override
    public Result payOff(@Valid PayOffParams payOffParams) throws Exception {
        Result<PayOrderDto> payOrderDtoResult = orderQueryClient.queryPayOrderByPayOrderNum(payOffParams.getPayOrderNum());
        if (payOrderDtoResult==null){
            throw new MallException("订单不存在",CommonResultCodeEnum.INVALID_PARAM);
        }
        if (payOrderDtoResult.getData().getPayWay().equals("ONLINE")){
            throw new MallException("支付方式错误",CommonResultCodeEnum.INVALID_PARAM);
        }
        orderManageClient.payOff(payOffParams.getPayOrderNum());
        return Result.success();
    }

    @Override
    public Result queryPayOrder() {
        Result<List<PayOrderDto>> listResult = orderQueryClient.storeQueryNoPayOrder(StoreIdUtil.getFromToken());
        return listResult;
    }

    @Override
    public Result orderItemDingOut(Integer itemId,Integer skuId, Integer tableId) {
        Result<TableClientDto> storeTableByTable = tableQueryClient.getStoreTableByTable(tableId);
        if (storeTableByTable.getData()==null){
            throw new MallException("餐桌不存在",CommonResultCodeEnum.INVALID_PARAM);
        }
        if (storeTableByTable.getData().getStoreId()!=StoreIdUtil.getFromToken()){
            throw new MallException("无退菜权限",CommonResultCodeEnum.INVALID_PARAM);
        }
        Result result = orderManageClient.orderItemDingOut(itemId,skuId, tableId);
        return result;
    }

}
